package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.QueryUserLocationVo;
import com.itheima.domain.vo.UserInfoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class UserLocationManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private UserLocationService userLocationService;

    /*
     *
     *  实战后台-分页查询用户详细数据
     * @param page 分页数据
     * @param pageSize  分页数据
     * @author
     * @date
     * @return com.itheima.domain.vo.PageBeanVo
     */

    public PageBeanVo findUserList(Integer page, Integer pageSize) {
        //查询分页数据
        PageBeanVo pageBeanVo = userInfoService.findUsersByPage(page, pageSize);

        //UserInfo数据
        List<UserInfo> items = (List<UserInfo>) pageBeanVo.getItems();

        //Vo集合
        List<UserInfoVo> userInfoVoList=new ArrayList<>();

        if(CollectionUtil.isNotEmpty(items)){

            for (UserInfo userInfo : items) {
                //Vo
                UserInfoVo userInfoVo = new UserInfoVo();

                //Vo赋值
                BeanUtil.copyProperties(userInfo,userInfoVo);

                userInfoVoList.add(userInfoVo);


            }

        }
        pageBeanVo.setItems(userInfoVoList);

        return pageBeanVo;
    }

    /*
     *
     *
     * @param userId    用户Id
     * @author
     * @date
     * @return com.itheima.domain.vo.UserLocationVo
     */

    public QueryUserLocationVo findUserLocation(Long userId) {

        //用户详细信息
        UserInfo userInfo = userInfoService.findById(userId);

        //用户具体位置信息
        Map<String, String> map = userLocationService.findByUserId(userId);

        QueryUserLocationVo vo=new QueryUserLocationVo();

        //用户id
        vo.setUserId(userInfo.getId());
        //用户昵称
        vo.setNickname(userInfo.getNickname());
        //头像
        vo.setAvatar(userInfo.getAvatar());
        if ("man".equals(userInfo.getGender())){
            //性别
            vo.setGender("男");
        }else if ("woman".equals(userInfo.getGender())){
            vo.setGender("女");
        }
        //城市
        vo.setCity(userInfo.getCity());
        //位置
        vo.setAddress(map.get("address"));
        //经度
        vo.setLongitude(Double.valueOf(map.get("longitude")));
        //维度
        vo.setLatitude(Double.valueOf(map.get("latitude")));

        return vo;

    }
}
